<template>
  <div class="box">
    <!-- 未登录头部 -->
    <div v-if="!userTokens" @click="$router.push('/login')" class="head no-login">
      <img src="~@/assets/mobile.png" alt="">
      <span>登录 / 注册</span>
    </div>
    <!-- 已登录 -->
    <div v-else class="head login">
      <div class="userInfo">
        <div class="left">
          <van-image
          class="img"
            round
            :src="userInfo.photo"
            />
            <span>{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button round size="mini" type="default">编辑资料</van-button>
        </div>
      </div>
      <div class="userData">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2">
        <van-grid-item>
          <i slot="icon" class="toutiao toutiao-shoucang"></i>
          <span slot="text" class="text">收 藏</span>
        </van-grid-item>
        <van-grid-item>
          <i slot="icon" class="toutiao toutiao-lishi"></i>
          <span slot="text" class="text">历 史</span>
        </van-grid-item>
    </van-grid>
    <!-- cell单元格 -->
    <van-cell-group class="cell-group">
      <van-cell title="消息通知" is-link/>
      <van-cell title="小智同学" is-link/>
      <van-cell v-if="userTokens" clickable class="cell-3" @click="logout" title="退 出"/>
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user/user'
export default {
  name: 'my',
  data () {
    return {
      userInfo: {}
    }
  },

  async created () {
    // 获取用户信息
    try {
      if (this.$store.state.userTokens) {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
      }
    } catch (err) {
      return this.$toast.fail('获取信息失败！')
    }
  },
  computed: {
    ...mapState(['userTokens'])
  },
  methods: {
    // 用户点击退出登录
    logout () {
      this.$dialog.confirm({
        message: '确认退出吗？退出后将无法享受部分权益！'
      })
        .then(() => {
          this.$store.commit('setToken', null)
        })
        .catch(() => {
          return false
        })
    }
  }
}
</script>

<style scoped lang='less'>
.box {
  .no-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      margin-top: 15px;
      font-size: 28px;
      color: #fff;
    }
  }
  .head {
    width: 750px;
    height: 401px;
    background-image: url('~@/assets/banner.png');
    background-size: contain;
  }
  .login {
    overflow: hidden;
    .userInfo {
      height: 180px;
      margin-top: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 32px;
      .left {
        display: flex;
        align-items: center;
        span {
          margin-left: 10px;
          color: #fff;
          font-size: 32px;
        }
      }
      .img {
        width: 132px;
        height: 132px;
      }
      .right {
        span.van-button__text {
          font-size: 24px;
        }
      }
    }
    .userData {
      display: flex;
      justify-content:space-around;
      align-items: center;
      height: 161px;
      .data-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: white;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 18px;
        }
        span:nth-child(2) {
          font-size: 32px;
        }
      }
    }
  }
  .van-grid {
    .van-grid-item {
      .toutiao {
        font-size: 45px;
      }
      i.toutiao-shoucang {
        color: #eb5253
      }
      i.toutiao-lishi {
        color: #ff9d1d
      }
      .text {
        font-size: 30px;
        margin-top: 16px;
      }
    }
  }
  .cell-group {
    margin-top: 20px;
  }
  .cell-3 {
    margin-top: 20px;
    text-align: center;
    color: red;

  }
}
</style>
